#include "ClockItem.hpp"
#include "Clock.hpp"
#include "AssetManager.hpp"
#include "Notification.hpp"
#include "Util.hpp"
#include <cpp3ds/System/I18n.hpp>
#include <cpp3ds/System/FileSystem.hpp>
#include <TweenEngine/Tween.h>
#include <time.h>
#include <stdlib.h>

namespace Clock {

ClockItem::ClockItem()
{
	m_clockDial.setOutlineThickness(1);
	m_clockDial.setOutlineColor(cpp3ds::Color(117, 117, 117));
	m_clockDial.setFillColor(cpp3ds::Color(158, 158, 158, 32));
	m_clockDial.setSize(cpp3ds::Vector2f(130.f, 130.f));
	m_clockDial.setOrigin(m_clockDial.getLocalBounds().width / 2, m_clockDial.getLocalBounds().height / 2);
	m_clockDial.setPosition(100.f, 120.f);

	m_clockScrew.setFillColor(cpp3ds::Color(97, 97, 97));
	m_clockScrew.setSize(cpp3ds::Vector2f(6.f, 6.f));
	m_clockScrew.setOrigin(m_clockScrew.getLocalBounds().width / 2, m_clockScrew.getLocalBounds().height / 2);
	m_clockScrew.setPosition(100.f, 120.f);

	m_secondHand.setPosition(100.f, 120.f);
	m_secondHand.setFillColor(g_themeColor);
	m_secondHand.setSize(cpp3ds::Vector2f(2.f, 50.f));
	m_secondHand.setOrigin(m_secondHand.getLocalBounds().width / 2, m_secondHand.getLocalBounds().height);

	m_minuteHand = m_secondHand;
	m_minuteHand.setFillColor(cpp3ds::Color(158, 158, 158));
	m_minuteHand.setSize(cpp3ds::Vector2f(2.f, 40.f));
	m_minuteHand.setOrigin(m_minuteHand.getLocalBounds().width / 2, m_minuteHand.getLocalBounds().height);

	m_hourHand = m_minuteHand;
	m_hourHand.setSize(cpp3ds::Vector2f(2.f, 30.f));
	m_hourHand.setOrigin(m_hourHand.getLocalBounds().width / 2, m_hourHand.getLocalBounds().height);

	m_textTwelve.setString("12");
	m_textTwelve.setCharacterSize(20);
	m_textTwelve.useSystemFont();
	m_textTwelve.setOrigin(m_textTwelve.getLocalBounds().width / 2, m_textTwelve.getLocalBounds().height / 2);
	m_textTwelve.setPosition(100.f, 67.f);
	m_textTwelve.setFillColor(cpp3ds::Color(97, 97, 97));

	m_textThree = m_textTwelve;
	m_textThree.setString("3");
	m_textThree.setOrigin(m_textThree.getLocalBounds().width / 2, m_textThree.getLocalBounds().height / 2);
	m_textThree.setPosition(154.f, 120.f);

	m_textSix = m_textTwelve;
	m_textSix.setString("6");
	m_textSix.setOrigin(m_textSix.getLocalBounds().width / 2, m_textSix.getLocalBounds().height / 2);
	m_textSix.setPosition(100.f, 171.f);

	m_textNein = m_textTwelve;
	m_textNein.setString("9");
	m_textNein.setOrigin(m_textNein.getLocalBounds().width / 2, m_textNein.getLocalBounds().height / 2);
	m_textNein.setPosition(45.f, 120.f);

	updateHands();
}

ClockItem::~ClockItem()
{

}

void ClockItem::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
{
	states.transform *= getTransform();

	target.draw(m_clockDial);

	target.draw(m_textTwelve);
	target.draw(m_textThree);
	target.draw(m_textSix);
	target.draw(m_textNein);

	target.draw(m_hourHand);
	target.draw(m_minuteHand);
	target.draw(m_secondHand);

	target.draw(m_clockScrew);
}

void ClockItem::update(float delta)
{
	m_tweenManager.update(delta);

	if (m_updateClock.getElapsedTime() >= cpp3ds::seconds(0.5f)) {
		m_updateClock.restart();

		updateHands();
	}
}

void ClockItem::updateHands()
{
	time_t t = time(NULL);
	struct tm * timeinfo;
	timeinfo = localtime(&t);

	m_secondHand.setRotation(timeinfo->tm_sec * 360 / 60);
	m_minuteHand.setRotation(timeinfo->tm_min * 360 / 60);

	int rotationDifference = ((timeinfo->tm_hour + 1) * 360 / 12) - (timeinfo->tm_hour * 360 / 12);
	m_hourHand.setRotation(((timeinfo->tm_hour % 12) * 360 / 12) + (timeinfo->tm_min * rotationDifference / 60));
}

} // namespace Clock
